package com.braveman.im.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.AddFriendsAndGroupBean;
import com.braveman.im.bean.DataListBean;
import com.braveman.im.bean.MineMomentBean;
import com.braveman.im.bean.SendLoveNotifyDetailsBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.FriendsMomentAdapter;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;


public class StateActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    private FriendsMomentAdapter friendsMomentAdapter;
    private RecyclerView mLvActivityListMember;
    private List<MineMomentBean.DataListBean> mineMomentList = new ArrayList<>();
    private LoadingLayout mLoadingLayout;
    private SwipeRefreshLayout mSwipeLayout;
    private int pageNo;
    private boolean isLastPage;
    private String time;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("我的动态").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(StateActivity.this);
            }
        });
        mTitleBarView.addActions(new TitleBarView.Action(R.mipmap.personal_dynamic_title_icon_photo) {
            @Override
            public void performAction(View view) {
                ActivityUtils.startActivity(StateActivity.this, SendMomentActivity.class);
            }
        });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_my_state;
    }

    @Override
    protected void initChildView() {
        friendsMomentAdapter = new FriendsMomentAdapter(R.layout.itme_friends_moment, R.layout.item_firend_moment_list_head, mineMomentList);
        mLoadingLayout = (LoadingLayout) findViewById(R.id.loading_layout);
        mLvActivityListMember = (RecyclerView) findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(this));
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
    }

    @Override
    protected void initChildData() {
        mLvActivityListMember.setAdapter(friendsMomentAdapter);
        doNetWork();
    }

    private void doNetWork() {
        ApiManager.getApi().getMineMomentList(pageNo, 10).enqueue(new NetCallBack<MineMomentBean>() {
            @Override
            protected void onSuccess(Call<MineMomentBean> call, Response<MineMomentBean> response) {
                if (response != null) {
                    friendsMomentAdapter.setEnableLoadMore(true);
                    MineMomentBean noticeBean = response.body();
                    if (noticeBean != null)
                        isLastPage = noticeBean.isLastPage;
                    if (pageNo == 1 && mineMomentList != null) {
                        mineMomentList.clear();

                    }
                    if (noticeBean != null) {
                        time = "";
                        for (MineMomentBean.DataListBean dataListBean : noticeBean.dataList) {
                            if (!time.equals(dataListBean.publishTimeDescription)) {
                                MineMomentBean.DataListBean data = new MineMomentBean.DataListBean(true, dataListBean.publishTimeDescription);
                                mineMomentList.add(data);
                            }
                            mineMomentList.add(dataListBean);
                            if (!TextUtils.isEmpty(dataListBean.publishTimeDescription)) {
                                time = dataListBean.publishTimeDescription;
                            } else {
                                time = "";
                            }
                        }

                    }

                    mLoadingLayout.showContent();

                    if (mineMomentList.size() == 0) {
                        mLoadingLayout.showEmpty();
                    } else if (pageNo == 1 && mineMomentList.size() < 10) {
                        friendsMomentAdapter.loadMoreEnd();
                    } else if (pageNo > 1 && mineMomentList.size() >= 10) {
                        friendsMomentAdapter.loadMoreEnd();
                    }
                    mSwipeLayout.setRefreshing(false);
                } else {

                    mLoadingLayout.showError();
                    mSwipeLayout.setRefreshing(false);
                }

                if (friendsMomentAdapter != null) {
                    friendsMomentAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        friendsMomentAdapter.setOnLoadMoreListener(this, mLvActivityListMember);
        friendsMomentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", mineMomentList.get(position).id);
                ActivityUtils.startActivity(StateActivity.this, MineMomentDetailsActivity.class, bundle);
            }
        });
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                doNetWork();
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
        if (isLastPage) {
            friendsMomentAdapter.loadMoreEnd();
        } else {
            pageNo++;
            doNetWork();
        }
    }

    @Override
    protected boolean hasEvent() {
        return super.hasEvent();
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode) {
            case EventCode.EVENT_UPDATA_DELETE_MOMENT:
                doNetWork();
                break;
        }
    }
}
